home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Miscellaneous / Randy Thelen / ThreadedBrot / Lib / AppleEventHandling.cp next >
Encoding:
Text File  |  1994-06-26  |  5.2 KB  |  186 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        AppleEventHandling.cp
  3.  
  4.     Contains:    Minimalist support for the required and Display Manager AppleEvents
  5.                 We also support openning and printing “LetterSpec” documents for AOCE.
  6.                 
  7.                 To really support AppleScript™, we’ll do ALOT more work in here.
  8.  
  9.     Written by: Dave Falkenburg
  10.  
  11.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  12.  
  13.     Change History (most recent first):
  14.      
  15.  */
  16.  
  17. #include <Types.h>
  18. #include <AppleEvents.h>
  19. #include <Errors.h>
  20. #include <Displays.h>            //    for Display Manager AppleEvent constants
  21. #include <OCEStandardMail.h>    //    for LetterSpec
  22.  
  23. #include "AppLib.h"
  24. #include "AppleEventHandling.h"
  25.  
  26. void
  27. InstallAppleEventHandlers(void)
  28.     {
  29.     //    It’s probably more efficient to use a table to install these handlers…
  30.     
  31.     (void) AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,NewAEEventHandlerProc(HandleOpenApplication),0,false);
  32.     (void) AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,NewAEEventHandlerProc(HandleOpenOrPrintDocuments),(long) &OpenDocument,false);
  33.     (void) AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,NewAEEventHandlerProc(HandleOpenOrPrintDocuments),(long) &PrintDocument,false);
  34.     (void) AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,NewAEEventHandlerProc(HandleQuitApplication),0,false);
  35.         
  36.     if (gHasDisplayManager)
  37.         (void) AEInstallEventHandler(kCoreEventClass,kAESystemConfigNotice,NewAEEventHandlerProc(HandleDisplayManagerNotice),0,false);
  38.     }
  39.  
  40.  
  41. OSErr
  42. CheckAppleEventForMissingParams(AppleEvent *theAppleEvent)
  43.     {
  44.     DescType    returnedType;
  45.     Size        actualSize;
  46.     OSErr        err;
  47.     
  48.     err = AEGetAttributePtr(theAppleEvent,keyMissedKeywordAttr,typeWildCard,
  49.                             &returnedType,nil,0,&actualSize);
  50.     
  51.     if (err == errAEDescNotFound)        //    If we couldn’t find the error attribute
  52.         return noErr;                    //        everything is ok, return noErr
  53.     
  54.     if (err == noErr)                    //    We found an error attribute, so
  55.         return errAEEventNotHandled;    //        tell the client we ignored the event
  56.  
  57.     return err;                            //    Something else happened, return it
  58.     }
  59.  
  60.  
  61. pascal OSErr
  62. HandleOpenApplication(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  63.     {
  64.     OSErr    err;
  65.     
  66.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  67.         return err;
  68.  
  69.     return noErr;
  70.  
  71.     return(OpenNewDocument());
  72.     }
  73.  
  74.  
  75. pascal OSErr
  76. HandleOpenOrPrintDocuments(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,EachDocumentProcPtr docProc)
  77.     {
  78.     AEDescList            documentList;
  79.     long                documentCount,documentIndex;
  80.     AEKeyword            keyword;
  81.     DescType            returnedType;
  82.     Size                actualSize;
  83.     LetterDescriptor    theLetterDesc;
  84.     OSErr                err;
  85.  
  86.     if ((err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&documentList)) != noErr)
  87.         return err;
  88.  
  89.  
  90.     // NOTE: If we are printing under GX we should get the printer info here
  91.     
  92.     
  93.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  94.         return err;
  95.     
  96.     if ((err = AECountItems(&documentList,&documentCount)) != noErr)
  97.         return err;
  98.     
  99.     for (documentIndex=1;documentIndex <= documentCount;documentIndex++)
  100.         {
  101.         //    What kind of document is it?
  102.         if ((err = AESizeOfNthItem(&documentList,documentIndex,&returnedType,&actualSize)) != noErr)
  103.             return err;
  104.         
  105.         //    Is it an AOCE letter?
  106.         if (returnedType == typeLetterSpec)
  107.             {
  108.             //    It’s a letter
  109.             theLetterDesc.onDisk = false;
  110.             err = AEGetNthPtr(&documentList,documentIndex,typeLetterSpec,&keyword,&returnedType,
  111.                                 (Ptr) &theLetterDesc.u.mailboxSpec, sizeof(theLetterDesc.u.mailboxSpec),&actualSize);
  112.             }
  113.         else
  114.             {
  115.             //    It’s just a normal document file
  116.             theLetterDesc.onDisk = true;
  117.             err = AEGetNthPtr(&documentList,documentIndex,typeFSS,&keyword,&returnedType,
  118.                                 (Ptr) &theLetterDesc.u.fileSpec,sizeof(theLetterDesc.u.fileSpec),&actualSize);
  119.             }
  120.             
  121.         if (err == noErr)
  122.             (*docProc)(&theLetterDesc);
  123.         }
  124.     
  125.     return noErr;
  126.     }
  127.  
  128.  
  129. pascal OSErr
  130. HandleQuitApplication(AppleEvent *theAppleEvent,AppleEvent * /* reply */,long /* refCon */)
  131.     {
  132.     OSErr    err;
  133.     
  134.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  135.         return err;
  136.  
  137.     gDone = QuitApplication();
  138.     
  139.     if (gDone)
  140.         return noErr;
  141.     else
  142.         return userCanceledErr;
  143.     }
  144.  
  145.  
  146. ////////////////////////////////////////////////////////////////////////
  147. //
  148. //    Display Manager Suite is “Under Construction”
  149. //
  150. //    To Do: Check ERS for Display Manager events
  151.  
  152. pascal OSErr
  153. HandleDisplayManagerNotice(AppleEvent *theAppleEvent,AppleEvent * /* reply */,long /* refCon */)
  154.     {
  155.     AEDescList    displayList;
  156.     long        displayCount,displayIndex;
  157.     OSErr        err;
  158.     
  159.     DebugStr((StringPtr) "\pGot Display Manager Event!");
  160.     
  161.     if ((err = AEGetParamDesc(theAppleEvent,kAEDisplayNotice,typeAEList,&displayList)) != noErr)
  162.         return err;
  163.     
  164.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  165.         return err;
  166.     
  167.     if ((err = AECountItems(&displayList,&displayCount)) != noErr)
  168.         return err;
  169.         
  170.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  171.         return err;
  172.  
  173.     for (displayIndex = 1; displayIndex<=displayCount; displayIndex++)
  174.         {
  175.         DebugStr((StringPtr) "\pProcessing a display");
  176.         
  177.         //    Check to see if a display has gone offline, if so, move windows that were on
  178.         //    that display to a better place on the remaining displays
  179.         
  180.         //    We probably want a utility method like TWindow::NudgeOnScreen() to share
  181.         //    with window positioning code as well.
  182.         }
  183.  
  184.     return errAEEventNotHandled;    //    we really don’t handle this yet
  185.     }
  186.